//
//  HomePageNoteViewController.m
//  product_B
//
//  Created by lanou on 16/5/28.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "HomePageNoteViewController.h"
#import "SmallScrollView.h"
#import "HomePageNoteWriteViewController.h"

@interface HomePageNoteViewController ()<ChangeNoteDelegate>
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)SmallScrollView *smallView;
@end

@implementation HomePageNoteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleLabel.text = @"小记";
    self.searchButton.frame = CGRectMake(20, 30, 50, 20);
    [self.searchButton setTitle:@"放弃" forState:UIControlStateNormal];
    [self.searchButton setTintColor:[UIColor grayColor]];
    [self.searchButton setBackgroundImage:nil forState:UIControlStateNormal];
    self.userButton.frame = CGRectMake(kWidth - 65, 30, 50, 20);
    [self.userButton setTitle:@"分享" forState:UIControlStateNormal];
    [self.userButton setTintColor:[UIColor grayColor]];
    [self.userButton setBackgroundImage:nil forState:UIControlStateNormal];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 74, kWidth-20, kHeight-74)];
    self.scrollView.contentSize = CGSizeMake(kWidth-20, kHeight+110);
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = YES;
    [self.view addSubview:_scrollView];
    
    self.smallView = [[UINib nibWithNibName:@"SmallScrollView" bundle:nil] instantiateWithOwner:self options:nil].firstObject;
    self.smallView.frame = CGRectMake(5, 5, kWidth-30, kHeight-230);
    _smallView.layer.shadowOffset = CGSizeMake(2, 2);
    _smallView.layer.shadowRadius = 5;
    _smallView.layer.shadowOpacity = 0.5;
    _smallView.layer.shadowColor = [UIColor blackColor].CGColor;
    _smallView.layer.cornerRadius = 5;
    [_smallView.picImageView sd_setImageWithURL:[NSURL URLWithString:self.model.hp_img_original_url]];
    _smallView.titleLabel.text = self.model.hp_title;
    _smallView.authorLabel.text = self.model.hp_author;
    _smallView.contentLabel.text = self.model.hp_content;
    CGFloat height = _model.hp_content.length /( (kWidth-50)/17) * 21;
    _smallView.frame = CGRectMake(5, 5, kWidth - 30, 10 + _smallView.picImageView.frame.size.height + 13 + _smallView.titleLabel.frame.size.height + 20 + height + kHeight / 667 * 50);
    if (_smallView.frame.size.height > kHeight - 160) {
        [_scrollView setContentSize:CGSizeMake(0, 10 + _smallView.picImageView.frame.size.height + 13 + _smallView.titleLabel.frame.size.height + 20 + height + 20 +  150 )];
    }
    
    [self.scrollView addSubview:_smallView];
    
    _smallView.contentLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [_smallView.contentLabel addGestureRecognizer:tap];
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    HomePageNoteWriteViewController *homePageNoteWriteVC = [[HomePageNoteWriteViewController alloc] init];
    homePageNoteWriteVC.contentString = _smallView.contentLabel.text;
    [self.navigationController pushViewController:homePageNoteWriteVC animated:YES];
    homePageNoteWriteVC.delegate = self;
    
}
- (void)changeContent:(NSString *)content{
    _smallView.contentLabel.text = content;
}
- (void)searchButtonAction:(UIButton *)sender{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定要放弃当前编辑的内容" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:nil];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"放弃" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    UIAlertAction *continueAction = [UIAlertAction actionWithTitle:@"继续" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancelAction];
    [alert addAction:continueAction];
}
- (UIImage *)captureWithView:(UIView *)view
{
    // 1.开启上下文
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
    
    // 2.将控制器view的layer渲染到上下文
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    // 3.取出图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 4.结束上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}
- (void)userButtonAction:(UIButton *)sender{
    UIImage *image = [self captureWithView:_smallView];
    NSArray *imageArray = @[image];
    if (imageArray) {
        
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:nil
                                         images:imageArray
                                            url:nil
                                          title:nil
                                           type:SSDKContentTypeAuto];
        //2、分享（可以弹出我们的分享菜单和编辑界面）//要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图，只有传这个才可以弹出我们的分享菜单，可以传分享的按钮对象或者自己创建小的view 对象，iPhone可以传nil不会影响
        
        [ShareSDK showShareActionSheet:nil
         
                                 items:nil
                           shareParams:shareParams
                   onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                       
                       switch (state) {
                           case SSDKResponseStateSuccess:
                           {
                               [self alert:@"分享成功"];
                               break;
                           }
                           case SSDKResponseStateFail:
                           {
                               [self alert:@"分享失败"];
                               break;
                           }
                           default:
                               break;
                       }
                   }
         ];}

}
- (void)alert:(NSString *)string{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:string preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:nil];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancel];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
